package controllers

import (
	"encoding/json"

	"github.com/astaxie/beego"
	"gitee.com/lzqysoft/ga_wz_backend/models"
)

// ServiceController operations for Service
type ServiceController struct {
	CommonController
}

// URLMapping ...
func (c *ServiceController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
}



// Post ...
// @Title Post
// @Description create Service
// @Param	body		body 	models.Service	true		"body for Service content"
// @Success 201 {int} models.Service
// @Failure 403 body is empty
// @router / [post]
func (c *ServiceController) Post() {
	user:= c.User
	sdata, err := c.AesDecodeToByte(c.GetString("tdata"))
	if err != nil {
		c.RetError(ErrInputData)
	}
	var form models.ServiceForm
	if err = json.Unmarshal(sdata, &form); err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	serv, err := models.GetServById(form.ServId)
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	}
	if err := models.AddServiceByServAndUser(serv, user, &form); err != nil {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	}
	c.RetSuccess("")

}

// GetOne ...
// @Title Get One
// @Description get Service by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Service
// @Failure 403 :id is empty
// @router /:id [get]
func (c *ServiceController) GetOne() {
	idStr := c.Ctx.Input.Param(":id")
	//id, _ := strconv.Atoi(idStr)
	v, err := models.GetServiceById(idStr)
	if err != nil {
		c.Data["json"] = err.Error()
	} else {
		c.Data["json"] = v
	}
	c.ServeJSON()
}

// GetAll ...
// @Title Get All
// @Description get Service
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 {object} models.Service
// @Failure 403
// @router /GetAll [post]
func (c *ServiceController) GetAll() {
	sdata, err := c.AesDecodeToByte(c.GetString("tdata"))
	var pageData PageData
	if err = json.Unmarshal(sdata, &pageData); err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	beego.Debug(pageData)
	l, err := models.GetAllService(pageData.Query, pageData.Fields, pageData.SortBy, pageData.Order, (pageData.Page-1)*pageData.PageSize, pageData.PageSize)
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	}
	beego.Debug(l)
	data := make(map[string]interface{})
	data["data"] = l
	data["total"] = len(l)
	c.RetSuccess(data)
}

// Put ...
// @Title Put
// @Description update the Service
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.Service	true		"body for Service content"
// @Success 200 {object} models.Service
// @Failure 403 :id is not int
// @router /:id [put]
func (c *ServiceController) Put() {
	idStr := c.Ctx.Input.Param(":id")
	//id, _ := strconv.Atoi(idStr)
	v := models.Service{Id: idStr}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		if err := models.UpdateServiceById(&v); err == nil {
			c.Data["json"] = "OK"
		} else {
			c.Data["json"] = err.Error()
		}
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// Delete ...
// @Title Delete
// @Description delete the Service
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *ServiceController) Delete() {
	idStr := c.Ctx.Input.Param(":id")
	//id, _ := strconv.Atoi(idStr)
	if err := models.DeleteService(idStr); err == nil {
		c.Data["json"] = "OK"
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}
